<template>
    <div class="recipesDetail">
        <div class="header">
            <div class="header_left">
                <img src="../assets/img/1.png" alt="" @click="goBack">
            </div>
            <div class="header_right">
                <img src="../assets/img/2.png" alt="">
                <img src="../assets/img/3.png" alt="">
                <img src="../assets/img/4.png" alt="">
            </div>
        </div>
        <div v-if="myloading" class="loading">
            <van-loading size="100px" color="#ec344e" />
        </div>
        <div class="bottom" v-else>
            <img :src="datas.recipe?.photo_path" alt="">
            <h1>{{ datas.recipe?.title }}</h1>
            <div class="text">
                <p>精品·浏览{{datas.recipe?.comments_count}}·收藏{{datas.recipe?.favo_counts}}·学做{{datas.recipe?.dish_count}}</p>
            </div>
            <div class="items">
                <div class="items_left">
                    <div class="user_photo">
                        <img :src="datas.recipe?.user?.user_photo" alt="">
                    </div>
                    <div class="user_name">
                        {{ datas.recipe?.user?.nickname }}
                    </div>
                    <div class="user_level">
                        Lv.{{ datas.recipe?.user?.lvl }}
                    </div>
                </div>
                <div class="items_right">
                    <button class="button">关注</button>
                </div>
            </div>
            <div class="text_box">
                <p>{{ datas.recipe?.cookstory }} <span>#kuailaiaiiai</span></p>
            </div>
            <div class="foodmenu">
                <h2>食材清单</h2>
                <span>加入采购清单</span>
            </div>
            <div v-for="(item,i) in datas.recipe?.major" :key="i" class="fooddetail">
                <div class="food_name">{{ item.title }}</div>
                <div class="food_count">{{ item.note }}</div>
            </div>
            <div class="foodsteps_menu">
                <h2>烹饪步骤</h2>
                <span>点击图片加入烹饪模式</span>
            </div>
            <div class="foodsteps">
                <div class="foodstep" v-for="(item,t) in datas.recipe?.cookstep" :key="t">
                    <p class="step_num">步骤{{ item.position }}</p>
                    <img :src="item.image" alt="">
                    <p class="step_text">{{ item.content }}</p>
                </div>
            </div>
            <div class="time">
                <p>{{ datas.recipe?.release_time }}</p>
            </div>
            <div class="recommend">
                <span>相关推荐</span>
            </div>
            <div class="hot_comments">
                <span class="hot">热门评论</span>
                <span class="hot_num">{{comments.cc}}条评论</span>
            </div>
            <div class="inp">
                <input type="text" placeholder="说点什么,让TA也认识爱做饭的你">
            </div>
            <div class="comments_text">
                <div v-if="comments.cc == 0" class="null_comments">{{ comments.el }}</div>
                <div v-else>
                    <div v-for="(item,d) in comments.comments" :key="d">
                        <div class="all_comments">
                            <div class="left">
                                <div class="user_photo">
                                    <img :src="item.u.p" alt="">
                                </div>
                                <div class="user_name">{{ item.u.n }}</div>
                                <div class="user_level">Lv.{{ item.u.lvl }}</div>
                            </div>
                            <div class="right">
                                <img src="../assets/img/collect.png" alt="">
                                <span>赞</span>
                            </div>
                        </div>
                        <p class="comment_text">{{ item.content[0]?.c }}</p>
                    </div>
                </div>
            </div>
            <div class="my_comment">
                <input type="text" placeholder="说点什么">
                <img src="../assets/img/5.png" alt="">
                <span>{{ comments.cc }}</span>
                <img src="../assets/img/collect_on.png" alt="" @click="collect" v-if="love">
                <img src="../assets/img/collect.png" alt="" @click="collect" v-else>
                <span>收藏</span>
                <img src="../assets/img/7.png" alt="">
                <span> 传学做</span>
            </div>
        </div>
    </div>
</template>

<script>
    import http from '@/api/request';
    export default {
        data(){
            return {
                datas: [],
                id: '',
                comments:[],
                myloading:false,
                love: false
            }
        },
        async created(){
            // 使用 $route.params 获取参数
            this.myloading = true;
            this.id = this.$route.params.keyword;
            await http.get(`https://apis.netstart.cn/douguo/recipe/detail/${this.id}`)
            .then(
                content => {
                    console.log('--->', content.result);
                    let banners = content.result;
                    this.datas = banners;
                    console.log(this.datas)
                }
            )
            .catch(error => {
                console.error("Error fetching data:", error);
            })

            await http.get(`https://apis.netstart.cn/douguo/recipe/flatcomments/${this.id}/0/20`)
            .then(
                content => {
                    console.log('--->', content.result);
                    let banner = content.result;
                    this.comments = banner;
                    console.log(this.comments)
                }
            )
            .catch(error => {
                console.error("Error fetching data:", error);
            })
            // 请求之后
            setTimeout(() => {
                // 隐藏loading
                this.myloading = false;
            }, 500);
        },
        methods: {
            goBack(){
                window.history.go(-1);
            },
            collect() {
                const arr = JSON.parse(localStorage.getItem('arr') || '[]'); // 初始为空数组或默认解析为JSON
                let img = this.datas.recipe?.photo_path;
                let recipe_name = this.datas.recipe?.title;
                let recipe_text = this.datas.recipe?.cookstory;
        
                let pl = { img, recipe_name, recipe_text };
        
                if (!Array.isArray(arr)) {
                    arr = [pl]; // 如果不是数组，则初始化为一个元素数组
                } else {
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].img === pl.img && arr[i].recipe_name === pl.recipe_name && arr[i].recipe_text === pl.recipe_text) {
                            console.log("已收藏");
                            this.love = true;
                            return;
                        }
                    }
        
                    if (this.love === false) {
                        arr.push(pl); // 如果未找到匹配项，添加新的条目
                    }
                }
        
                localStorage.setItem('arr', JSON.stringify(arr)); // 更新本地存储
                this.love = true;
            }
        },
    }
</script>

<style lang="less">
@import '../common/less/varible.less';
    .recipesDetail{
        width: 100%;
        .header{
            width: 100%;
            display: flex;
            padding: 20px;
            justify-content: space-between;
            position:fixed;
            left: 0;
            top: 0;
            background-color: @bgColor;
            .header_left{
                img{
                    width: 25px;
                    height: 25px;
                }
            }
            .header_right{
                display: flex;
                img{
                    width: 25px;
                    height: 25px;
                    margin-left: 15px;

                }
            }
        }
        .loading{
            width: 100%;
            margin-top: 200px;
            padding-left: 35%;
        }
        .bottom{
            img{
                width: 100%;
            }
            h1{
                padding: 15px;
                font-size: 24px;
            }
            .text{
                p{
                    margin-left: 15px;
                    font-size: 12px;
                    color: #787878;
                    font-weight: 600;
                }
            }
            .items{
                display: flex;
                justify-content: space-between;
                width: 100%;
                padding: 15px;
                .items_left{
                    display: flex;
                    align-items: center;
                    .user_photo{
                        img{
                            width: 30px;
                            height: 30px;
                            border-radius: 50%;
                        }
                    }
                    .user_name{
                        font-size: 12px;
                        margin-left: 10px;
                    }
                    .user_level{
                        font-size: 10px;
                        margin-left: 10px;
                        color: #c7b50a;
                    }
                }
                .items_right{
                    .button{
                        background-color: #ffc533;
                        width: 70px;
                        height: 30px;
                        border-radius: 15px;
                        text-align: center;
                        line-height: 30px;
                        font-size: 14px;
                        font-weight: 600;
                    }
                }
            }
            .text_box{
                padding: 10px;
                width: 100%;
                margin-top: 15px;
                margin-bottom: 10px;
                p{
                    font-size: 14px;
                    span{
                        color: #569aa2;
                    }
                }
            }
            .foodmenu{
                display: flex;
                justify-content: space-between;
                padding: 0 10px;
                h2{
                    font-size: 14px;
                }
                span{
                    font-size: 12px;
                    color: #569aa2;
                }
            }
            .fooddetail{
                display: flex;
                justify-content: space-between;
                margin: 12px 15px;
                .food_name{
                    color:#569aa2;
                    font-size: 12px;
                }
                .food_count{
                    font-size: 12px;
                }
            }
            .foodsteps_menu{
                display: flex;
                justify-content: space-between;
                padding: 0 10px;
                h2{
                    font-size: 14px;
                }
                span{
                    font-size: 12px;
                    color: #569aa2;
                }
            }
            .foodsteps{
                .foodstep{
                    .step_num{
                        margin: 12px 10px;
                        font-size: 12px;
                        font-weight: 600;
                    }
                    img{
                        width: 94%;
                        margin: 0 10px;
                        border-radius: 10px;
                    }
                    .step_text{
                        margin: 12px 10px;
                        font-size: 12px;
                    }
                }
            }
            .time{
                p{
                    font-size: 12px;
                    color: #ccc;
                    margin: 12px 10px;
                }
            }
            .recommend{
                span{
                    font-weight: 600;
                    margin-left: 10px;
                    font-size: 14px;
                }
            }
            .hot_comments{
                margin-top: 20px;
                display: flex;
                align-items: center;
                .hot{
                    margin-left: 10px;
                    font-weight: 600;
                    font-size: 14px;
                }
                .hot_num{
                    margin-left: 10px;
                    font-size: 12px;
                    color: #569aa2;
                }
            }
            .inp{
                margin-top: 20px;
                margin-left: 10px;
                input{
                    padding: 10px;
                    width: 90%;
                    height: 32px;
                    background-color: #f5f5f5;
                    border-radius: 20px;
                    border: none;
                    font-size: 12px;
                }
            }
            .comments_text{
                margin-top: 20px;
                margin-left: 10px;
                margin-right: 10px;
                margin-bottom: 80px;
                .null_comments{
                    margin: 30px 0;
                    text-align: center;
                    font-weight: 600;
                    font-size: 30px;
                }
                .all_comments{
                    margin-top: 20px;
                    display: flex;
                    justify-content: space-between;
                    margin-left: 10px;
                    margin-right: 10px;
                    align-items: center;
                    .left{
                        display: flex;
                        align-items: center;
                        .user_photo{
                            img{
                                width: 20px;
                                height: 20px;
                            }
                        }
                        .user_name{
                            font-size: 12px;
                            margin-left: 10px;
                        }
                        .user_level{
                            font-size: 8px;
                            margin-left: 10px;
                            color: #ffc533;
                        }
                    }
                    .right{
                        img{
                            width: 15px;
                            height: 15px;
                        }
                        span{
                            font-size: 12px;
                        }
                    }
                }
                .comment_text{
                    margin-top: 20px;
                    margin-left: 40px;
                    margin-bottom: 20px;
                    font-size: 12px;
                }
            }
            .my_comment{
                position: fixed;
                bottom: 0;
                left: 0;
                width: 100%;
                border-top: 0.8px solid #eee;
                height: 50px;
                background-color: #eee;
                input{
                    margin-top: 8px;
                    margin-bottom: 8px;
                    margin-left: 15px;
                    height: 34px;
                    width: 200px;
                    border-radius: 17px;
                    background-color: #f5f5f5;
                    border: none;
                    padding-left: 10px;
                    font-size: 12px;
                }
                img{
                    width: 18px;
                    height: 16px;
                    margin-left: 5px;
                }
                span{
                    font-size: 12px;
                }
            }
        }
    }
</style>